#!/bin/bash
# date: Mon Aug  1 16:01:18 CST 2022
# author: xkl
# 交互界面
read -p "选择节点类型 (1.master  2.slave 3.退出) " node
	case $node in
		1)			
			systemctl set-default graphical.target
                	echo -e "\e[32m已为master设置图形界面\e[0m"
			;;
		2)
			systemctl set-default multi-user.target
                	echo -e "\e[32m已为slave设置文本界面\e[0m"
			;;
		3)
			break;
			;;
		*)
			echo -e "\e[31m请按要求输入\e[0m"
			;;
	esac
# 设置主机名和ip地址
read -p "输入修改的主机名" hostname
hostnamectl set-hostname $hostname

ipaddr=`ip addr | grep ens160 | grep inet | awk '{print $2}'`
gateway=`route -n | awk '$NF~/ens160/{print($2)}' | grep "^[^0]"`
if [ -n $ipaddr ];then
read -p "你当前的ip为$ipaddr,网关为$gateway,请输入你想设置的 ip地址/：子网掩码" nipaddr
nmcli connection modify ens160 autoconnect yes ipv4.method manual ipv4.addresses $nipaddr ipv4.gateway $gateway ipv4.dns 114.114.114.114
nmcli connection up ens160
echo -e "\e[32mip地址修改成功,现在的ip为`ip addr | grep ens160 | grep inet | awk '{print $2}'`\e[0m"
else
	echo -e "\e[31m网络错误！\e[0m"
	exit 1
fi

# 添加主机与IP地址映射
echo -en "127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4\n::1         localhost localhost.localdomain localhost6 localhost6.localdomain6\n`ip addr | grep "ens160" | grep inet | awk '{print($2)}' | awk -F '/' '{print($1)}'` `hostname`\n" > /etc/hosts
echo -e "\e[32m映射完成\e[0m"

# 清空用户名为student开头的用户，创建student,密码1,设置sudo免密登陆

username=`awk -F : '{print $1}' /etc/passwd | grep "^student"`
if [[ -z $username ]];then
	echo -e "\e[31m不存在student开头的用户\e[0m"
else
	for i in $username
	do
		userdel -r $i
	done
	echo -e "\e[32mstudent用户已删除成功\e[0m"
fi

echo -e "\e[33m重新创建用户student...\e[0m"
useradd student
echo "1" | passwd --stdin student
studentvar=`grep "^student" /etc/sudoers`
if [ -z $studentvar ];then
	sed -i "/^root/a student ALL=(ALL) NOPASSWD:ALL" /etc/sudoers
	echo -e "\e[32mstudent已创建成功，初始密码1,免密设置完毕\e[0m"
else
	echo -e "\e[32mstudent已创建成功，初始密码1,免密设置完毕\e[0m"
fi
